package com.example.classmanageapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.classmanageapp.sqlite.MyDatabaseHelper;
import com.example.classmanageapp.utils.SysUtils;

public class LoginActivity extends Activity {

    private Button btn_login;
    private AutoCompleteTextView userName;
    private EditText userPassword;
    private RadioButton rbtnUserLogin;
    private RadioButton rbtnTchLogin;
    private RadioGroup rg;
    private MyDatabaseHelper dbHelper;
    private Integer type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //读取背景色
        SharedPreferences sharedPreferences = this.getSharedPreferences("loginData", MODE_PRIVATE);
        String currentColor = sharedPreferences.getString("currentColor", "-1");
        SysUtils.setStatusBarColor(this, true, 0);
        SysUtils.setSkin(this, Integer.parseInt(currentColor));


        btn_login = (Button) findViewById(R.id.login);
        userName = (AutoCompleteTextView) findViewById(R.id.userName);
        userPassword = (EditText) findViewById(R.id.userPassword);
        rg = (RadioGroup) findViewById(R.id.RadioGroup);
        rbtnUserLogin = (RadioButton) findViewById(R.id.RbtnUserLogin);
        rbtnTchLogin = (RadioButton) findViewById(R.id.RbtnTchLogin);

        dbHelper = new MyDatabaseHelper(this, "COURSE_SCHEDULE.db", null, 1);
        //type 默认登录账号类型是 0 学生
        type = 0;
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == rbtnUserLogin.getId()) {
                    //Toast.makeText(LoginActivity.this, "rbtnUserLogin", Toast.LENGTH_LONG).show();
                    type = 0;
                }
                if (checkedId == rbtnTchLogin.getId()) {
                    //Toast.makeText(LoginActivity.this, "rbtnTchLogin", Toast.LENGTH_LONG).show();
                    type = 1;
                }
            }

        });
        btn_login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                SQLiteDatabase db = dbHelper.getReadableDatabase();
                String sql = "SELECT * FROM tb_userlogin where tb_userlogin.username = ? and tb_userlogin.passpword = ? and tb_userlogin.usertype = ?";
                Cursor cursor = db.rawQuery(sql, new String[]{userName.getText().toString(), userPassword.getText().toString(), type.toString()});

                //选中教师
                if (type == 1) {
                    if (cursor.moveToFirst()) {
                        Intent intent = new Intent(LoginActivity.this,
                                MainTeacherActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名或者密码输入错误，请重新输入", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    if (cursor.moveToFirst()) {
                        Intent intent = new Intent(LoginActivity.this,
                                MainStudentActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名或者密码输入错误，请重新输入", Toast.LENGTH_SHORT).show();
                    }
                }
                cursor.close();
            }
        });


    }


}
